package com.servlet;



import com.dao.Signin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

@WebServlet("/signin")
public class SigninServlet extends HttpServlet {



    static {


        try {
            Class.forName("org.h2.Driver");
            Connection conn = DriverManager.getConnection("jdbc:h2:mem:test");
            Statement statement = conn.createStatement();
            statement.executeUpdate("create table users" +
                    "(\n" +
                    "    id       int primary key, \n"+
                    "    name     varchar(40) ,\n" +
                    "    password varchar(40) ,\n" +
                    "    email    varchar(60) ,\n" +
                    "    birthday date        \n" +
                    ");");
            statement.executeUpdate("INSERT INTO users (id, name, password, email, birthday) VALUES (1, 'ww', '111', null, null);");

            statement.executeUpdate("create table student\n" +
                    "(\n" +
                    "    sid       char(6) primary key,\n" +
                    "    studentno varchar(20)  ,\n" +
                    "    sname     varchar(10)  ,\n" +
                    "    gender    varchar(10)  ,\n" +
                    "    age       int          ,\n" +
                    "    address   varchar(100) \n" +
                    ");");

            statement.executeUpdate("INSERT INTO student (sid, studentno, sname, gender, age, address) VALUES ('1', '15', 'a', '男', 11, '1');");

            statement.executeUpdate("create table if not exists studentClass\n" +
                    "(\n" +
                    "    id   char(6) primary key,\n" +
                    "    name varchar(10) not null,\n" +
                    "    studentClass_sid char comment '学生表 id外键',\n" +
                    "    foreign key (studentClass_sid) references student(sid)\n" +
                    ");");

            statement.executeUpdate("insert into studentClass (id, name)\n" +
                    "values (1, 1931);");
            statement.executeUpdate("insert into studentClass (id, name)\n" +
                    "values (2, 1932);");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }






    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    private void heServlet(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, ServletException {
        req.setCharacterEncoding("UTF-8");
        String userName = req.getParameter("username");
        String passWord = req.getParameter("password");
        System.out.println("username = " + userName);
        System.out.println("password = " + passWord);

        //登录
        Signin signin = new Signin();
        boolean b = signin.loginCode(userName, passWord);
        System.out.println(b);
        if (b){
            resp.sendRedirect("Studentmanagement.jsp");
            System.out.println("登录成功");
        }else {
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            System.out.println("登录失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            heServlet(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
